package org.joy;

import okhttp3.*;

import java.io.File;
import java.io.IOException;

public class PostClient {
    public static void main(String[] args) throws IOException {
        String url = "http://joydemo.xyz/oss";
        OkHttpClient client = new OkHttpClient();

        File file = new File("D:/demo.jpg");
        RequestBody filebody = RequestBody.create(MediaType.parse("image/jpeg"),file);
        System.out.println(filebody.contentLength());
        RequestBody body = new MultipartBody.Builder()
                .addFormDataPart("username","admin")
                .addFormDataPart("password","123456")
                .addFormDataPart("file","filename",filebody)
                .setType(MediaType.parse("multipart/form-data"))
                .build();

        Request requst = new Request.Builder()

                .url(url)
                .post(body)
                .build();
        System.out.println(requst.body().contentType().toString());
        client.newCall(requst).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });
    }
}
